﻿using ConfClient;
using SvCore.Tcp;
using System.Collections;
using System.Collections.Generic;
using System.Security.Cryptography;
using UnityEngine;
using UtilLib;
using Zpb;
using static ConfClient.Conf;
using static GameDefine;
using static GameData;
using static Zui;
using static UnityEditor.Progress;

public class WdEquips : MonoBehaviour
{
    public ModButton ModButton;
    public Component ObjsContent;
    public Component ObjsActs;
    private Dictionary<long, ModButton> DictButton = new Dictionary<long, ModButton>();

    // Start is called before the first frame update
    void Start()
    {

    }

    // Update is called once per frame
    void Update()
    {

    }
    public void WdShow()
    {
        this.Show();
        //UsageLine.Hide();

        ShowList();
    }
    public void ShowList()
    {
        ObjsContent.transform.DestroyChildren();
        DictButton.Clear();

        foreach (var item in GameData.PlayerInfoEquips.Equips.Values)
        {
            if (!ConfEquip.TryGetValue(item.ConfigId, out var conf))
            {
                return;
            }
            var btn = ObjsContent.InstAdd(ModButton);
            btn.SetTags(item, conf);
            btn.SetOnClick(OnClickBtn);
            DictButton[item.Uid] = btn;
            ShowItem(btn);
        }
    }
    public void ShowItem(ModButton btn)
    {
        var item = btn.ModTags[0] as EquipInfo;
        var conf = btn.ModTags[1] as ConfigEquip;
        if (PlayerWeared(item.Uid))
        {
            btn.Text = $"{conf.Name} √";
        }
        else
        {
            btn.Text = $"{conf.Name}";
        }
        if ((EEquipType)conf.EquipType == EEquipType.武器)
        {
            btn.SetButtonColor(Zsc.Color(255, 153, 102));
        }
        else
        {
            btn.SetButtonColor(Zsc.Color(0, 255, 0));
        }
        btn.FontSize = 24;
    }

    public void OnClickBtn(ModButton btn)
    {
        ObjsActs.transform.DestroyChildren();

        List<EActType> listActs = new List<EActType>();
        listActs.Add(EActType.查看);
        var item = btn.ModTags[0] as EquipInfo;
        var conf = btn.ModTags[1] as ConfigEquip;
        listActs.Add(EActType.丢弃);

        var actWear = EActType.装备;
        if (PlayerWeared(item.Uid)) 
        {
            actWear = EActType.卸下;
        }
        listActs.Add(actWear);

        foreach (var act in listActs)
        {
            var btnAct = ObjsActs.InstAdd(ModButton);
            btnAct.Text = $"{act}";
            btnAct.SetButtonColor(Zsc.Color(0, 128, 255));
            btnAct.FontSize = 36;
            btnAct.Button.SetHeight(60);
            btnAct.SetTags(act, item, conf);
            btnAct.SetOnClick(OnClickBtnAct);
        }
    }
    public void OnClickBtnAct(ModButton btn)
    {
        var act = (EActType)((int)btn.ModTags[0]);
        var item = btn.ModTags[1] as EquipInfo;
        var conf = btn.ModTags[2] as ConfigEquip;
        switch (act)
        {
            case EActType.查看:
                MsgShow(item.LookDesc());
                break;
            case EActType.丢弃:
                MySocket.Send(new C2GEquipDrop() { Uid = item.Uid });
                break;
            case EActType.装备:
                MySocket.Send(new C2GEquipWear() { Uid = item.Uid });
                break;
            case EActType.卸下:
                MySocket.Send(new C2GEquipOff() { Uid = item.Uid });
                break;
            default:
                break;
        }
    }
    public void G2CEquips(MyPackage pack)
    {
        pack.GetMsg(out G2CEquips rsp);

        GameData.PlayerInfoEquips = rsp.InfoEquips;

        Zui.Uim.Window.Show(rsp.Tag);
    }
    public void G2CEquipDrop(MyPackage pack)
    {
        pack.GetMsg(out G2CEquipDrop rsp);
        if (DictButton.TryGetValue(rsp.Uid, out var btn))
        {
            btn.gameObject.DestroyOne();
            ObjsActs.transform.DestroyChildren();
        }
    }
    public void G2CEquipWear(MyPackage pack)
    {
        pack.GetMsg(out G2CEquipWear rsp);
        // 新装备
        if (rsp.Uids.Count >= 1)
        {
            if (DictButton.TryGetValue(rsp.Uids[0], out var btn))
            {
                ShowItem(btn);
                OnClickBtn(btn);
                //btn.Button.Select();
            }
        }
        // 旧装备
        if (rsp.Uids.Count >= 2)
        {
            if (DictButton.TryGetValue(rsp.Uids[1], out var btn))
            {
                ShowItem(btn);
            }
        }
    }
    public void G2CEquipOff(MyPackage pack)
    {
        pack.GetMsg(out G2CEquipOff rsp);
        if (DictButton.TryGetValue(rsp.Uid, out var btn))
        {
            ShowItem(btn);
            OnClickBtn(btn);
            //btn.Button.Select();
        }
    }
}
